package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HInventoryProjectMarkModel;
import com.zhongwang.mms.module.half.service.InvToProExcService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:库存划项目执行
 * @author: th
 * @time: 2020/5/7 8:22
 */
@Controller
@RequestMapping("/half/halInvManage/invToProExc")
public class InvToProExcController extends BaseController {

    //页面文件路径
    private String prefix = "half/halInvManage/invToProExc";
    @Autowired
    private InvToProExcService invToProExcService;

    /**
     * 加载页面
     *
     * @param model
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halwaprorexec:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 加载页面数据
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions({"halinv:halwaprorexec:view"})
    public JsonResult execList(HInventoryProjectMarkModel query) {

        startPage();
        return jsonResult(invToProExcService.selectHInventoryProjectMarkByQuery(query));
    }

    /**
     * 库存划项目执行
     * @param hInventoryProjectMarkModel
     * @return
     */
    @PostMapping("/execApply")
    @ResponseBody
    @RequiresPermissions({"halinv:halwaprorexec:edit"})
    public JsonResult execApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        if (invToProExcService.execApply(hInventoryProjectMarkModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 库存划项目撤销执行
     * @param hInventoryProjectMarkModel
     * @return
     */
    @PostMapping("/revokeApply")
    @ResponseBody
    @RequiresPermissions({"halinv:halwaprorexec:edit"})
    public JsonResult revokeApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        if (invToProExcService.revokeApply(hInventoryProjectMarkModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail("因库存划项目需求的库存划项目库存信息因被标记或库存不足导致无法撤销！");
    }

    /**
     * 驳回
     * @param param
     * @return
     */
    @PostMapping("/invalid")
    @ResponseBody
    @RequiresPermissions({"halinv:halwaprorexec:edit"})
    public JsonResult invalid(HInventoryProjectMarkModel param) {
        if (invToProExcService.invalid(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
}
